import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:lib_bt/bean/BleBeanInterface.dart';
import 'package:lib_bt/listener/OnOtaListener.dart';
import 'package:lib_util/ToastUtil.dart';
import 'package:lib_wake_lock/lib_wake_lock.dart';
import '../../../../../AppGlobals.dart';
import '../../../../../generated/l10n.dart';
import '../../base/LedBleBeanInterface.dart';
import '../BleBean.dart';
import 'package:lib_bt/BleManager.dart';

import 'OtaUtil.dart';

class OtaDialog extends ChangeNotifier implements OnOtaListener {
  var otaLoading;
  LibWakeLock _fluWakeLock = LibWakeLock.instance;

  bool _isBootLoader = true;
  int _progress = 0;

  bool get isBootLoader => _isBootLoader;

  set isBootLoader(bool value) {
    _isBootLoader = value;
    notifyListeners();
  }

  int get progress => _progress;

  set progress(int value) {
    _progress = value;
    notifyListeners();
  }

  //单例
  static OtaDialog? _instance;

  static OtaDialog get instance => _instance ??= OtaDialog._();

  OtaDialog._() {
    BleManager.instance.addOtaListener(this);
  }

  @override
  void onOtaBootLoaderProgress(int progress) {
    isBootLoader = true;
    this.progress = progress;
  }

  @override
  void onOtaProgress(int progress) {
    isBootLoader = false;
    this.progress = progress;
  }

  @override
  void onOtaStart() {
    showOtaDialog();
  }

  @override
  void onOtaSuccess() {
    hideOtaDialog();
    ToastUtil.showToast(S.of(AppGlobals.rootContext).str_common_18);
  }

  void showOtaDialog() {
    if (otaLoading != null) {
      return;
    }
    _fluWakeLock.enable();
    otaLoading = BotToast.showCustomLoading(
        clickClose: false,
        toastBuilder: (cancelFunc) {
          return ListenableBuilder(
              listenable: this,
              builder: (context, child) {
                return Column(children: [
                  const Expanded(child: SizedBox()),
                  Expanded(
                      child: Container(
                          margin: const EdgeInsets.only(left: 20, right: 20),
                          padding: const EdgeInsets.only(left: 20, right: 20),
                          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10)),
                          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
                            Text(S.of(context).str_common_20,
                                style: const TextStyle(fontSize: 20, color: Colors.black)),
                            const SizedBox(height: 20),
                            Text(isBootLoader ? S.of(context).str_common_21 : S.of(context).str_common_22),
                            const SizedBox(height: 10),
                            LinearProgressIndicator(
                                value: progress / 100,
                                minHeight: 10,
                                borderRadius: BorderRadius.circular(50),
                                backgroundColor: Colors.grey.withAlpha(50),
                                valueColor: const AlwaysStoppedAnimation(Colors.blue))
                          ]))),
                  const Expanded(child: SizedBox())
                ]);
              });
        },
        onClose: () {
          otaLoading = null;
          progress = 0;
        });
  }

  bool isOTA() {
    return otaLoading != null;
  }

  void hideOtaDialog() {
    if (otaLoading != null) {
      otaLoading();
      otaLoading = null;
      _fluWakeLock.disable();
    }
  }

  @override
  void onOtaFailed(String msg) {
    hideOtaDialog();
    ToastUtil.showToast(S.of(AppGlobals.rootContext).str_common_19);
  }

  @override
  void onOtaAvailable(String address, String mac, bool isMandatoryUpgrade) {
    var bleBean = BleManager.instance.getBleBean<LedBleBeanInterface>(address);
    if (bleBean == null) {
      return;
    }
    if (isMandatoryUpgrade) {
      print("强制升级");
      OtaUtil.startOta(bleBean);
    } else {
      OtaUtil.saveOtaInfo(bleBean);
    }
  }
}
